//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RatePseudoRootJacobianNumerical.h"
using namespace Cephei::QL::Models::Marketmodels::Pathwisegreeks;
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (Cephei::QL::Math::IMatrix^ pseudoRoot, UInt64 aliveIndex, UInt64 numeraire, Cephei::Core::IVector<Double>^ taus, Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ pseudoBumps, Cephei::Core::IVector<Double>^ displacements) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMatrix^ _CpseudoRoot;
    try
    {
#ifdef HANDLE
        _phRatePseudoRootJacobianNumerical = NULL;
#endif
        _CpseudoRoot = safe_cast<CMatrix^> (pseudoRoot);
        _CpseudoRoot->Lock();
        QuantLib::Matrix& _pseudoRoot = static_cast<QuantLib::Matrix&> (_CpseudoRoot->GetReference ()); 
        QuantLib::Size _aliveIndex = (QuantLib::Size)ValueHelper::Convert (aliveIndex); //d
        QuantLib::Size _numeraire = (QuantLib::Size)ValueHelper::Convert (numeraire); //d
        taus->Lock();
        INativeVector<Double>^ _NCItaus = taus->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCtaus = safe_cast<CDoubleVector^>(_NCItaus);
        std::vector<QuantLib::Time>& _taus = static_cast<std::vector<QuantLib::Time>&> (_NCtaus->GetReference ());
        pseudoBumps ->Lock ();
        INativeVector<Cephei::QL::Math::IMatrix^>^ _NCIpseudoBumps = pseudoBumps->getFeature (NativeFeature::Value);
        CMatrixVector^ _NCpseudoBumps = safe_cast<CMatrixVector^>(_NCIpseudoBumps);
        std::vector<QuantLib::Matrix>& _pseudoBumps = static_cast<std::vector<QuantLib::Matrix>&> (_NCpseudoBumps->GetReference ());
        displacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = displacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        _ppRatePseudoRootJacobianNumerical = new boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical> (new QuantLib::RatePseudoRootJacobianNumerical ( _pseudoRoot,  _aliveIndex,  _numeraire,  _taus,  _pseudoBumps,  _displacements ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CpseudoRoot != nullptr) _CpseudoRoot->Unlock();
        if (taus != nullptr) taus->Unlock();    //not optional
        if (pseudoBumps != nullptr) pseudoBumps->Unlock();    //not optional
        if (displacements != nullptr) displacements->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobianNumerical = NULL;
#endif
	_ppRatePseudoRootJacobianNumerical = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (QuantLib::RatePseudoRootJacobianNumerical& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRatePseudoRootJacobianNumerical = NULL;
#endif
	_ppRatePseudoRootJacobianNumerical = new boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical> (&childNative);
    
    _RatePseudoRootJacobianNumericalOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (CRatePseudoRootJacobianNumerical^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobianNumerical = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRatePseudoRootJacobianNumerical = new boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobianNumerical = NULL;
#endif
	if (!t->IsSubclassOf(CRatePseudoRootJacobianNumerical::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (QuantLib::Handle<QuantLib::RatePseudoRootJacobianNumerical>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRatePseudoRootJacobianNumerical = &childNative;
	_ppRatePseudoRootJacobianNumerical = &static_cast<boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical>>(childNative.currentLink());
    
    _RatePseudoRootJacobianNumericalOwner = owner;
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (QuantLib::Handle<QuantLib::RatePseudoRootJacobianNumerical> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRatePseudoRootJacobianNumerical = &childNative;
	_ppRatePseudoRootJacobianNumerical = &static_cast<boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::CRatePseudoRootJacobianNumerical (QuantLib::RatePseudoRootJacobianNumerical childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRatePseudoRootJacobianNumerical = NULL;
#endif
	_ppRatePseudoRootJacobianNumerical = new boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical> (new QuantLib::RatePseudoRootJacobianNumerical (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::~CRatePseudoRootJacobianNumerical ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRatePseudoRootJacobianNumerical != NULL)
    {
	    delete _ppRatePseudoRootJacobianNumerical;
        _ppRatePseudoRootJacobianNumerical = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::!CRatePseudoRootJacobianNumerical ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRatePseudoRootJacobianNumerical != NULL)
    {
	    delete _ppRatePseudoRootJacobianNumerical;
    }
}
QuantLib::RatePseudoRootJacobianNumerical& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::GetReference ()
{
    if (_ppRatePseudoRootJacobianNumerical == NULL) throw REFNEW NativeNullException ();
	return **_ppRatePseudoRootJacobianNumerical;
}
boost::shared_ptr<QuantLib::RatePseudoRootJacobianNumerical>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::GetShared ()
{
    if (_ppRatePseudoRootJacobianNumerical == NULL) throw REFNEW NativeNullException ();
	return *_ppRatePseudoRootJacobianNumerical;
}
QuantLib::RatePseudoRootJacobianNumerical* Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::GetPointer ()
{
    if (_ppRatePseudoRootJacobianNumerical == NULL) throw REFNEW NativeNullException ();
	return &**_ppRatePseudoRootJacobianNumerical;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RatePseudoRootJacobianNumerical>& Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::GetHandle ()
{
	if (_phRatePseudoRootJacobianNumerical == NULL)
	{
		_phRatePseudoRootJacobianNumerical = new Handle<QuantLib::RatePseudoRootJacobianNumerical> (*_ppRatePseudoRootJacobianNumerical);
	}
	return *_phRatePseudoRootJacobianNumerical;
}
#endif
bool Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::HasNative () 
{
	return (_ppRatePseudoRootJacobianNumerical != NULL);
}

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IRatePseudoRootJacobianNumerical^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical::GetBumps (Cephei::Core::IVector<Double>^ oldRates, Cephei::Core::IVector<Double>^ oneStepDFs, Cephei::Core::IVector<Double>^ newRates, Cephei::Core::IVector<Double>^ gaussians, Cephei::QL::Math::IMatrix^ B)
{
    CMatrix^ _CB;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        oldRates->Lock();
        INativeVector<Double>^ _NCIoldRates = oldRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NColdRates = safe_cast<CDoubleVector^>(_NCIoldRates);
        std::vector<QuantLib::Rate>& _oldRates = static_cast<std::vector<QuantLib::Rate>&> (_NColdRates->GetReference ());
        oneStepDFs->Lock();
        INativeVector<Double>^ _NCIoneStepDFs = oneStepDFs->getFeature (NativeFeature::Value);
        CDoubleVector^ _NConeStepDFs = safe_cast<CDoubleVector^>(_NCIoneStepDFs);
        std::vector<QuantLib::Real>& _oneStepDFs = static_cast<std::vector<QuantLib::Real>&> (_NConeStepDFs->GetReference ());
        newRates->Lock();
        INativeVector<Double>^ _NCInewRates = newRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnewRates = safe_cast<CDoubleVector^>(_NCInewRates);
        std::vector<QuantLib::Rate>& _newRates = static_cast<std::vector<QuantLib::Rate>&> (_NCnewRates->GetReference ());
        gaussians->Lock();
        INativeVector<Double>^ _NCIgaussians = gaussians->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgaussians = safe_cast<CDoubleVector^>(_NCIgaussians);
        std::vector<QuantLib::Real>& _gaussians = static_cast<std::vector<QuantLib::Real>&> (_NCgaussians->GetReference ());
        _CB = safe_cast<CMatrix^> (B);
        _CB->Lock();
        QuantLib::Matrix& _B = static_cast<QuantLib::Matrix&> (_CB->GetReference ()); 
    	(*_ppRatePseudoRootJacobianNumerical)->getBumps ( _oldRates,  _oneStepDFs,  _newRates,  _gaussians,  _B );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (oldRates != nullptr) oldRates->Unlock(); //NO2  
        if (oneStepDFs != nullptr) oneStepDFs->Unlock(); //NO2  
        if (newRates != nullptr) newRates->Unlock(); //NO2  
        if (gaussians != nullptr) gaussians->Unlock(); //NO2  
        if (_CB != nullptr) _CB->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Pathwisegreeks::IRatePseudoRootJacobianNumerical^ Cephei::QL::Models::Marketmodels::Pathwisegreeks::CRatePseudoRootJacobianNumerical_Factory::Create (Cephei::QL::Math::IMatrix^ pseudoRoot, UInt64 aliveIndex, UInt64 numeraire, Cephei::Core::IVector<Double>^ taus, Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ pseudoBumps, Cephei::Core::IVector<Double>^ displacements)
{
    return REFNEW CRatePseudoRootJacobianNumerical ( pseudoRoot,  aliveIndex,  numeraire,  taus,  pseudoBumps,  displacements);
}
